/*
 * Copyright 2006 Robert Burrell Donkin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package rat.report.xml.visitor.report.license;

import java.io.IOException;
import java.io.Reader;

import rat.element.ElementVisitorImpl;
import rat.element.StandardContentElement;
import rat.license.ILicense;
import rat.license.ILicenseMatcher;
import rat.report.ReportFailedException;
import rat.report.ReportFailedRuntimeException;
import rat.report.claim.IClaimReporter;

public class LicenseReportVisitor extends ElementVisitorImpl {


    private static final String HEADER_SAMPLE = "header-sample";
    private static final String HEADER_TYPE = "header-type";
    
    private final IClaimReporter reporter;
    private final ILicenseMatcher matcher;
    
    public LicenseReportVisitor(final IClaimReporter reporter, final ILicenseMatcher matcher) {
        this.reporter = reporter;
        this.matcher = matcher;
    }

    public void visit(StandardContentElement standardContent) {
        try {
            final Reader reader = standardContent.getReader();
            LicenseCheckWorker worker = new LicenseCheckWorker(reader, matcher);
            worker.read();
            final ILicense license = worker.getLicense();
            final String name = standardContent.getName();
            report(license, name);
        } catch (IOException e) {
            throw new ReportFailedRuntimeException(e);
        }
    }
    
    void report(ILicense license, String subject) {
        final String name = license.getName();
        final String code = license.getCode();
        final String notes = license.getNotes();
        final boolean isAcceptable =  license.isAcceptable();
        final boolean isGenerated = license.isGenerated();
        try {
            if (isGenerated) {
                reporter.claim(subject, HEADER_TYPE, "GEN  ", false);
                writeHeaderSample(notes, subject);
            } else {
                reporter.claim(subject, HEADER_TYPE, code, false);
                writeHeaderSample(notes, subject);
                reporter.claim(subject, "license-family", name, false);
                final String approvalValue = Boolean.toString(isAcceptable);
                // TODO: not very readable; 
                // TODO: replace when license approval factored into separate phase
                // TODO: probably name='ASF'
                reporter.claim(subject, "license-approval", approvalValue, false);
            }
        } catch (ReportFailedException e) {
            // Cannot recover
            throw new ReportFailedRuntimeException("Cannot print license information", e);
        }
    }

    private void writeHeaderSample(final String notes, final String subject) throws ReportFailedException {
        reporter.claim(subject, HEADER_SAMPLE, notes, true);
    }

}
